//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by 胡阳 on 16/5/19.
//  Copyright © 2016年 young4ever. All rights reserved.
//  appKey:    3399256692
//  appSecret: 9afae57e602c45ea5d42c665b131febd
//  加载登录页: https://api.weibo.com/oauth2/authorize?client_id=3399256692&redirect_uri=http://www.vcredit.com
//  已经授权的requestToken: d0add1f544c96b68559cc940cb2e24c3

import UIKit

let WBSwitchRootViewControllerNotification = "WBSwitchRootViewController"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.switchRootViewController(_:)), name: WBSwitchRootViewControllerNotification, object: nil)
        
        // 设置导航条的tabbar的外观
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        //1. 创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //2. 设置根视图控制器
        window?.rootViewController = getDefaultViewController()
        window?.makeKeyAndVisible()
        
        isNewVersionToUpdate()
        
        return true
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func switchRootViewController(notify: NSNotification)  {
        
        if notify.object as! Bool {
            
            window?.rootViewController = MainViewController()
        }else {
            window?.rootViewController = WelcomeViewController()
        }
        
    }
    
    /**
     获取默认界面
     */
    private func getDefaultViewController() -> UIViewController
    {
        // 判断用户是否登录
        if UserAccount.userLogin() {
            
            // 有新版本就是新特性界面 , 没有就是欢迎界面
            return isNewVersionToUpdate() ? NewFeatureViewController() : WelcomeViewController()
        }
        
        // 用户未登录就是我们的主界面<tabBarController>
        return MainViewController()
    }
    
    /**
     检查版本更新
     */
    private func isNewVersionToUpdate() -> Bool
    {
        // 1.获取当前软件版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String ;
        
        // 2.获取以前的软件版本号
        /*
            ?? 表示前面的值为nil就取后面的值(eg: var str = nil ?? "123")
         */
        let sandboxVersion = NSUserDefaults.standardUserDefaults().objectForKey("CFBundleShortVersionString") as? String ?? ""
//        print("current \(currentVersion) sandbox \(sandboxVersion)")
        // 3.比较版本号<当前版本大于以前版本则有更新>
        if currentVersion.compare(sandboxVersion) == NSComparisonResult.OrderedDescending {
            
            // 在iOS7 以后 NSUserDefaults 存数据不需要同步(synchronize)了,
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "CFBundleShortVersionString")
            return true
        }
        
        return false
    }
}

